home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / AEswitchControlTemplate.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  13.4 KB  |  447 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //
  20. //  Creation Date:    March 6, 1998
  21. //  Author:        cdt
  22. //
  23. //  Procedure Name:
  24. //        AEswitchControlTemplate
  25. //
  26. //  Description Name;
  27. //        Creates the custom attribute controls for the switch node.
  28. //
  29. //  Input Value:
  30. //        nodeName        (currently unused)
  31. //      inputAttr
  32. //      shapeAttr
  33. //      shadeAttr
  34. //
  35. //  Output Value:
  36. //        None
  37. // 
  38.  
  39. proc string switchPlug( string $switchNode,
  40.                         string $inputAttr,
  41.                         string $inputIndex,
  42.                         string $compoundAttr )
  43. {
  44.     return ($switchNode+"."+$inputAttr+"["+$inputIndex+"]."+$compoundAttr);
  45. }
  46.  
  47. proc addLast( string $itemArray[], string $item )
  48. {
  49.     $itemArray[size($itemArray)] = $item;
  50. }
  51.  
  52. proc string plugAttr( string $plug )
  53. {
  54.     string $buffer[];
  55.     tokenize($plug, ".", $buffer);
  56.     return $buffer[1];
  57. }
  58.  
  59. proc downStreamNetwork( string $node, string $result[] )
  60. {
  61.     if (`objectType $node` == "shadingEngine") {
  62.         // Check if the node is already in the result. This may occur if
  63.         // the outputs from a node branch and feed back into the same
  64.         // shading engine. For example, a switch node connected to a
  65.         // placement.
  66.  
  67.         int $isFound = false;
  68.  
  69.         for ($item in $result) {
  70.             if ($item == $node) {
  71.                 $isFound = true;
  72.                 break;
  73.             }
  74.         }
  75.  
  76.         if (!$isFound) addLast($result, $node);
  77.     }
  78.     else {
  79.         string $connections[] = `listConnections -connections true
  80.             -source false -destination true $node`;
  81.  
  82.         // Foreach destination (output) connection on node.
  83.         for ($i = 0; $i < size($connections); $i += 2) {
  84.             string $srcPlug = $connections[$i];
  85.             string $dstNode = $connections[$i + 1];
  86.  
  87.             // Is it not a message connection ?
  88.             if (plugAttr($srcPlug) != "message") {
  89.                 // We follow the first non-message output connection.
  90.                 downStreamNetwork($dstNode, $result);
  91.             }
  92.         }
  93.     }
  94. }
  95.  
  96. //
  97. //  Procedure Name:
  98. //      AEswitchRemoveCB
  99. //
  100. //  Description:
  101. //      Procedure to remove a row from the switch table.
  102. //
  103. global proc AEswitchRemoveCB( string $shapePlug,
  104.                               string $shadePlug)
  105. {
  106.     string $shapeCons[] = `listConnections -plugs true $shapePlug`;
  107.  
  108.     for ($item in $shapeCons) {
  109.         disconnectAttr $item $shapePlug;
  110.     }
  111.  
  112.     string $shadeCons[] = `listConnections -plugs true $shadePlug`;
  113.  
  114.     for ($item in $shadeCons) {
  115.         disconnectAttr $item $shadePlug;
  116.     }
  117. }
  118.  
  119. //
  120. //  Procedure Name:
  121. //      AEswitchMenuCB
  122. //
  123. //  Description:
  124. //      Procedure to build a popup menu specific to the spread sheet
  125. //      row the mouse is above. This menu will be displayed when the
  126. //      user presses the RMB.
  127. //
  128. global proc AEswitchMenuCB( string $parent,
  129.                             string $menuName,
  130.                             string $inputAttr,
  131.                             string $shapeAttr,
  132.                             string $shadeAttr )
  133. {
  134.     setParent $parent;
  135.  
  136.     setParent -m $menuName;
  137.  
  138.     popupMenu -e -deleteAllItems $menuName;
  139.  
  140.     string $switchNode = `switchTable -q -switchNode switchTable`;
  141.  
  142.     int $row = `switchTable -q -underPointerRow switchTable`;
  143.  
  144.     if ($row == -1) return;
  145.  
  146.     // Create shape and shade plugs for the element under the pointer.
  147.     //
  148.  
  149.     string $shapePlug = switchPlug($switchNode, $inputAttr, $row, $shapeAttr);
  150.     string $shadePlug = switchPlug($switchNode, $inputAttr, $row, $shadeAttr);
  151.  
  152.     // Create menu item to show the AE of the nodes connected to shape plug
  153.     //
  154.  
  155.     string $shapeCons[] = `listConnections $shapePlug`;
  156.  
  157.     for ($item in $shapeCons) {
  158.         menuItem -l ("Edit "+$item+" ...") -c ("showEditor "+$item);
  159.     }
  160.  
  161.     // Create menu item to show the AE of the nodes connected to shade plug
  162.     //
  163.  
  164.     string $shadeCons[] = `listConnections -plugs true $shadePlug`;
  165.  
  166.     for ($item in $shadeCons) {
  167.         menuItem -l ("Edit "+$item+" ...") -c ("showEditor "+$item);
  168.     }
  169.  
  170.     // Create a menu item to map a new shader to the shade plug
  171.     //
  172.  
  173.     menuItem -l "Map ..."
  174.         -c ("createRenderNode -allWithTexturesUp "+
  175.             ("\"defaultNavigation -connectToExisting -source %node "+
  176.              "-destination "+$shadePlug+"\" ")+
  177.             "\"\"");
  178.  
  179.     // Create a menu item to disconnect the shape and shade plugs.
  180.     //
  181.  
  182.     menuItem -l "Remove item"
  183.         -c ("AEswitchRemoveCB "+$shapePlug+" "+$shadePlug);
  184. }
  185.  
  186. //
  187. //  Procedure Name:
  188. //      AEswitchAddButtonCB
  189. //
  190. //  Description:
  191. //      Procedure to connect all surfaces downstream of the
  192. //      switch. This be invoked from the add button in the AE.
  193. //
  194. global proc AEswitchAddButtonCB( string $switchNode,
  195.                                  string $inputAttr,
  196.                                  string $shapeAttr,
  197.                                  string $shadeAttr )
  198. {
  199.     int $nextAvailable = 0;
  200.  
  201.     string $currentSurfaces[];
  202.  
  203.     // Create the current list of surfaces. We filter switchConnections
  204.     // because it will contain connections to both the shape and shade.
  205.     //
  206.  
  207.     string $switchConnections[] = `listConnections -connections true
  208.         -plugs true ($switchNode+"."+$inputAttr)`;
  209.  
  210.     for ($i = 0; $i < size($switchConnections); $i += 2) {
  211.         // Is this a shape connection ?
  212.         if (match("\\."+$shapeAttr+"$", $switchConnections[$i]) != "") {
  213.             addLast($currentSurfaces, $switchConnections[$i+1]);
  214.         }
  215.     }
  216.  
  217.     // Search downstream to find all shading groups
  218.     //
  219.  
  220.     string $shadingGroups[];
  221.     downStreamNetwork( $switchNode, $shadingGroups );
  222.  
  223.     if (size($shadingGroups) == 0) {
  224.         error -showLineNumber false
  225.             ($switchNode+" is not connected to a shading group,"+
  226.              " unable to add surfaces");
  227.     }
  228.  
  229.     // Foreach shading group add the surfaces not already connected
  230.     //
  231.  
  232.     int $wereSurfacesAdded = false;
  233.  
  234.     for ($group in $shadingGroups) {
  235.         string $surfaces[] =
  236.             `listConnections -plugs true ($group+".dagSetMembers")`;
  237.  
  238.         for ($surface in $surfaces) {
  239.             // Ensure the surface is not already connected
  240.             //
  241.  
  242.             int $isFound = false;
  243.  
  244.             for ($currentSurface in $currentSurfaces) {
  245.                 if ($surface == $currentSurface) {
  246.                     $isFound = true;
  247.                     break;
  248.                 }
  249.             }
  250.  
  251.             if (!$isFound) {
  252.                 $wereSurfacesAdded = true;
  253.  
  254.                 // Find the next available connection slot
  255.                 //
  256.  
  257.                 string $shapePlug, $shadePlug;
  258.  
  259.                 while (true) {
  260.                     $shapePlug = switchPlug( $switchNode, $inputAttr,
  261.                                              $nextAvailable, $shapeAttr );
  262.  
  263.                     $shadePlug = switchPlug( $switchNode, $inputAttr,
  264.                                              $nextAvailable, $shadeAttr );
  265.  
  266.                     string $shape[] = `listConnections $shapePlug`;
  267.  
  268.                     string $shade[] = `listConnections $shadePlug`;
  269.  
  270.                     // Is there no connections to the shape and shade ?
  271.                     if (size($shape) == 0 && size($shade) == 0)
  272.                         break;
  273.  
  274.                     $nextAvailable += 1;
  275.                 }
  276.  
  277.                 connectAttr $surface $shapePlug;
  278.  
  279.                 // The same surface cannot be in two shading groups,
  280.                 // but it doesn't hurt to ensure we don't add it
  281.                 // again.
  282.  
  283.                 addLast($currentSurfaces, $surface);
  284.             }
  285.         }
  286.     }
  287.  
  288.     if (!$wereSurfacesAdded) {
  289.         warning -showLineNumber false
  290.             ("There are no more surfaces to add to "+$switchNode+","+
  291.              " they have already been added");
  292.     }
  293. }
  294.  
  295. //
  296. //  Procedure Name:
  297. //      AEswitchRemoveButtonCB
  298. //
  299. //  Description:
  300. //      Procedure to break connections to the shape and shade of the
  301. //      selected switch table row. This will be invoked from the
  302. //      remove button in the AE.
  303. //
  304. global proc AEswitchRemoveButtonCB( string $switchNode,
  305.                                     string $inputAttr,
  306.                                     string $shapeAttr,
  307.                                     string $shadeAttr )
  308. {
  309.     int $row = `switchTable -q -selectedRow switchTable`;
  310.  
  311.     if ($row == -1) return;
  312.  
  313.     string $shapePlug = switchPlug($switchNode, $inputAttr, $row, $shapeAttr);
  314.  
  315.     string $shadePlug = switchPlug($switchNode, $inputAttr, $row, $shadeAttr);
  316.  
  317.     AEswitchRemoveCB($shapePlug, $shadePlug);
  318. }
  319.  
  320. //
  321. //  Procedure Name:
  322. //      AEswitchMapButtonCB
  323. //
  324. //  Description:
  325. //      Procedure to create a new shader and connect it to the
  326. //      selected switch table row. This will be invoked from the map
  327. //      button in the AE.
  328. //
  329. global proc AEswitchMapButtonCB( string $switchNode,
  330.                                  string $inputAttr,
  331.                                  string $shapeAttr,
  332.                                  string $shadeAttr )
  333. {
  334.     int $row = `switchTable -q -selectedRow switchTable`;
  335.  
  336.     if ($row == -1) return;
  337.  
  338.     string $shadePlug = switchPlug($switchNode, $inputAttr, $row, $shadeAttr);
  339.  
  340.     createRenderNode -allWithTexturesUp
  341.         ("defaultNavigation -connectToExisting -source %node -destination "+
  342.          $shadePlug) "";
  343. }
  344.  
  345. //
  346. //  Procedure Name:
  347. //      AEswitchControlNew
  348. //
  349. //  Description:
  350. //      Procedure to create the switch table for a switch node.
  351. //
  352. global proc AEswitchControlNew( string $inputAttr,
  353.                                 string $shapeAttr,
  354.                                 string $shadeAttr,
  355.                                 string $messagePlug )
  356. {
  357.     global int $gTextColumnWidthIndex;
  358.     global int $gAESingleWidgetWidthIndex;
  359.  
  360.     separator -style "in";
  361.  
  362.     int    $switchButtonSize = ((3 * $gAESingleWidgetWidthIndex) / 2 );
  363.  
  364.     rowLayout -numberOfColumns 3 
  365.         -cw 1 $gTextColumnWidthIndex 
  366.         -cw 2 $switchButtonSize 
  367.         -cw 3 $switchButtonSize
  368.  
  369.         -cal 1 "center" -cal 2 "center" -cal 3 "center"
  370.         -cat 1 "right" 1 -cat 2 "both" 1 -cat 3 "both" 1;
  371.         button -l "Add Surfaces" -w $switchButtonSize addButton;
  372.         button -l "Remove Item" removeButton;
  373.         button -l "Map Item" mapButton;
  374.         setParent ..;
  375.  
  376.     separator -style "none";
  377.  
  378.     formLayout switchForm;
  379.         switchTable switchTable;
  380.         popupMenu -button 3 switchMenu;
  381.         setParent ..;
  382.  
  383.     formLayout -e
  384.         -height 150
  385.         -af switchTable top 0
  386.         -af switchTable left ($gTextColumnWidthIndex - $switchButtonSize)
  387.         -af switchTable bottom 0
  388.         -af switchTable right 0
  389.         switchForm;
  390.  
  391.     separator -style "in";
  392.  
  393.     AEswitchControlReplace($inputAttr, $shapeAttr, $shadeAttr, $messagePlug);
  394. }
  395.  
  396. //
  397. //  Procedure Name:
  398. //      AEswitchControlNew
  399. //
  400. //  Description:
  401. //      Procedure to initialize the switch table for a node.
  402. //
  403. global proc AEswitchControlReplace( string $inputAttr,
  404.                                     string $shapeAttr,
  405.                                     string $shadeAttr,
  406.                                     string $messagePlug )
  407. {
  408.     string $buffer[];
  409.     tokenize($messagePlug, ".", $buffer);
  410.     string $nodeName = $buffer[0];
  411.  
  412.     switchTable -e
  413.         -label1 $shapeAttr -label2 $shadeAttr
  414.         -switchNode $nodeName
  415.         switchTable;
  416.  
  417.     string $parent = `setParent -q`;
  418.     string $menuName = $parent + "|switchForm|switchTable|switchMenu";
  419.     popupMenu -e -postMenuCommand
  420.         ("AEswitchMenuCB "
  421.          +$parent+" "+$menuName+" "+$inputAttr+" "+$shapeAttr+" "+$shadeAttr)
  422.         switchMenu;
  423.  
  424.     button -e -command
  425.         ("AEswitchAddButtonCB "+$nodeName+" "+ $inputAttr+" "+
  426.          $shapeAttr+" "+$shadeAttr) addButton;
  427.  
  428.     button -e -command
  429.         ("AEswitchRemoveButtonCB "+$nodeName+" "+$inputAttr+" "+
  430.          $shapeAttr+" "+ $shadeAttr) removeButton;
  431.  
  432.     button -e -command
  433.         ("AEswitchMapButtonCB "+$nodeName+" "+$inputAttr+" "+
  434.          $shapeAttr+" "+ $shadeAttr) mapButton;
  435. }
  436.  
  437. global proc AEswitchControlTemplate( string $nodeName,
  438.                                      string $inputAttr,
  439.                                      string $shapeAttr,
  440.                                      string $shadeAttr)
  441. {
  442.     editorTemplate -callCustom
  443.         ("AEswitchControlNew "+$inputAttr+" "+$shapeAttr+" "+$shadeAttr)
  444.         ("AEswitchControlReplace "+$inputAttr+" "+$shapeAttr+" "+$shadeAttr)
  445.         "message";
  446. }
  447.